package com.runchamp;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Oryan on 15/12/2014.
 */
public class JSONAdapterParticipantsList extends BaseAdapter {

    Context mContext;
    LayoutInflater mInflater;
    JSONArray mJsonArray;

    public JSONAdapterParticipantsList(Context context, LayoutInflater inflater) {
        mContext = context;
        mInflater = inflater;
        mJsonArray = new JSONArray();
    }

    @Override
    public int getCount() {
        return mJsonArray.length();
    }

    //////BaseAdapter functions///////

    @Override
    public Object getItem(int position) {
        return mJsonArray.optJSONObject(position);
    }

    @Override
    public long getItemId(int position) {
        JSONObject json = (JSONObject) getItem(position);
        try {
            long key = json.getLong("Key");
            return key;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        // check if the view already exists, if so, no need to inflate and findViewById again!
        if (convertView == null) {

            // Inflate the custom row layout from your XML.
            convertView = mInflater.inflate(R.layout.row_participant, null);
            // create a new "Holder" with subviews
            holder = new ViewHolder();
            holder.nameTextView = (TextView) convertView.findViewById(R.id.text_user_name);
            holder.gradeTextView = (TextView) convertView.findViewById(R.id.text_grade);
            holder.readyTextview = (TextView) convertView.findViewById(R.id.text_ready);
            // hang onto this holder for future recyclage
            convertView.setTag(holder);
        } else {

            // skip all the expensive inflation/findViewById and just get the holder you already made
            holder = (ViewHolder) convertView.getTag();
        }

        // Get the current participant's data in JSON form
        JSONObject jsonObject = (JSONObject) getItem(position);

        // Grab the name and grade from the JSON
        String name = "";
        String grade = "";
        boolean ready = false;

        if (jsonObject.has("Name")) {
            name = jsonObject.optString("Name");
        }

        if (jsonObject.has("Grade")) {
            grade = jsonObject.optString("Grade");
        }

        if (jsonObject.has("Ready")) {
            ready = jsonObject.optBoolean("Ready");
        }

        // Send these Strings to the TextViews for display
        holder.nameTextView.setText(name);
        holder.gradeTextView.setText(grade);

        //change ready color
        if (ready)
            holder.readyTextview.setTextColor(Color.argb(255, 63, 179, 0));
        else
            holder.readyTextview.setTextColor(Color.argb(255, 179, 179, 179));

        return convertView;
    }

    public void updateData(JSONArray jsonArray) {
        // update the adapter's dataset
        mJsonArray = jsonArray;
        notifyDataSetChanged();
    }


    //////Update///////

    public boolean isUserInRace(JSONArray jsonArray, long userKey) {
        for (int i = 0; i < getCount(); i++) {
            if (getItemId(i) == userKey)
                return true;
        }
        return false;
    }

    //////Participants functions///////

    public boolean isUserReady(JSONArray jsonArray, long userKey) {

        for (int i = 0; i < getCount(); i++) {
            if (getItemId(i) == userKey) {
                JSONObject json = (JSONObject) getItem(i);
                try {
                    boolean ready = json.getBoolean("Ready");
                    return ready;
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return false;
            }

        }

        return false;
    }

    private static class ViewHolder {
        public TextView nameTextView;
        public TextView gradeTextView;
        public TextView readyTextview;
    }
}
